﻿#region Copyright Notice
// 
// 
// Copyright (c) 2009-2010 Anindya Chatterjee
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// 
// 
#endregion
using System;
using System.Collections.Generic;
using Event.Plugin;

namespace DesktopNotifier
{
    /// <summary>
    /// Main functional class, which will constantly listened to the events and
    /// raises alarm when the event is captured
    /// </summary>
    internal class EventListener
    {
        static EventListener()
        {
            EventRepository = new List<IEvent>();
        }

        /// <summary>
        /// Repsitory for all the functional events
        /// </summary>
        internal static List<IEvent> EventRepository { get; set; }

        /// <summary>
        /// Makes the event ready for listening, and put item into the event repository
        /// </summary>
        /// <param name="iEvent"></param>
        internal static void RegisterEvent(IEvent iEvent)
        {
            EventRepository.Add(iEvent);
            iEvent.Listened += Listened;
            if (iEvent.IsScheduled && iEvent.Condition != null)
            {
                iEvent.Condition.Listened += Listened;
                iEvent.Condition.StartListening();
            }
            else
                iEvent.StartListening();
        }

        /// <summary>
        /// Executes itself when an event is captured. If the event is a Condition event, then
        /// it will take the parent event of it and put them into the repo for listening. If it is
        /// a normal event than call the Notify method of it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private static void Listened(object sender, SignalArgs args)
        {
            IEvent iEvent = args.Sender;
            if (iEvent is AbstractCondition)
            {
                iEvent.EndListening();
                ((AbstractCondition) iEvent).Parent.StartListening();
            }
            else
            {
                if (iEvent.IsScheduled)
                {
                    if (iEvent.Loop)
                    {
                        //Looping Scheduled Event
                        iEvent.Condition.StartListening();
                        iEvent.EndListening();
                    }
                    else
                    {
                        //Non-Looping Scheduled Event
                        iEvent.Condition.EndListening();
                        iEvent.EndListening();
                    }
                }
                    //Non-Looping UnScheduled Event
                else if (!iEvent.IsScheduled && !iEvent.Loop)
                    iEvent.EndListening();

                // Looping UnScheduled Event will go on, so nothing to stop or start here

                // after handling all these looping and stoping, notify from the event
                ((AbstractEvent) iEvent).Notify();
            }
        }

        internal static bool RemoveEvent(string name)
        {
            foreach (IEvent item in EventRepository)
            {
                if (item.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                {
                    EventRepository.Remove(item);
                    return true;
                }
            }
            return false;
        }
    }
}